package com.kejiguan.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.kejiguan.ui.R;
import com.kejiguan.ui.response.AlarmResponse;

/**
 * Created by YYY on 16/7/9.
 */
public class AlarmDialog extends Dialog implements View.OnClickListener{


    private View mDialogView;
    private Button okBtn;
    private TextView alarmInfoView;
    private View closeBtn;
    private AlarmResponse alarmResponse;
    private OnAlarmDialogDismissListener onAlarmDialogDismissListener;

    public AlarmDialog(Context context) {
        super(context);
        init(context);
    }

    public AlarmDialog(Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    protected AlarmDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
        setCanceledOnTouchOutside(true);
    }

    private void init(Context context)
    {
        mDialogView= LayoutInflater.from(context).inflate(R.layout.dialog_alarm_layout,null);
        alarmInfoView= (TextView) mDialogView.findViewById(R.id.alarm_text);
        okBtn= (Button) mDialogView.findViewById(R.id.alarm_ok_btn);
        closeBtn=mDialogView.findViewById(R.id.close_btn);
        closeBtn.setOnClickListener(this);
        setContentView(mDialogView);
        setCanceledOnTouchOutside(true);
    }

    public AlarmDialog withAlarm(AlarmResponse response)
    {
        this.alarmResponse=response;
        alarmInfoView.setText(alarmResponse.getAlarmContent());

        return this;
    }

    public AlarmDialog withOnClick(View.OnClickListener onClick)
    {
        okBtn.setOnClickListener(onClick);
        return this;
    }

    public AlarmDialog withOnAlarmDialogDismissListener(OnAlarmDialogDismissListener onAlarmDialogDismissListener)
    {
        this.onAlarmDialogDismissListener=onAlarmDialogDismissListener;
        return this;
    }

    @Override
    public void dismiss()
    {
        super.dismiss();
        if (null!=onAlarmDialogDismissListener)
        {
            onAlarmDialogDismissListener.onAlarmDismiss();
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.close_btn:
                dismiss();
                break;
        }
    }


    public interface OnAlarmDialogDismissListener
    {
        public void onAlarmDismiss();
    }
}
